/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.MachineManagement;

import Utilities.Constants.MachineSnapshotManagementAction;
import Utilities.Functions;
import Utilities.PermissionsConstants;

/**
 * Instances of this class are used by the user interface when a request to
 * manage a virtual machine's snapshots is sent to the machines management service.
 *
 * @author Angel Sanadinov
 */
public class ManageMachineSnapshotsRequest extends MachineManagementRequest
{
    private MachineSnapshotManagementAction snapshotManagementAction;//the snapshot management action
    private String snapshotName;        //the name of the snapshot (create, modify)
    private String snapshotDescription; //the snapshot description (create, modify)
    private String snapshotId;          //the id of the snapshot   (modify, remove, restore)

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     *
     * @see MachineSnapshotManagementAction#INVALID_ACTION
     */
    public ManageMachineSnapshotsRequest()
    {
        super();
        this.snapshotManagementAction = MachineSnapshotManagementAction.INVALID_ACTION;
        this.snapshotName = null;
        this.snapshotDescription = null;
        this.snapshotId = null;
        setRequestedAction(PermissionsConstants.ACTION_MACHINE_SNAPSHOT_MANAGE);
    }

    /**
     * Private constructor for setting up the snapshot management request.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the machine resides
     * @param machineId the id of the snapshot parent machine
     * @param action the requested management action
     * @param name snapshot name (if available)
     * @param description snapshot description (if available)
     * @param id snapshot UUID (if available)
     */
    private ManageMachineSnapshotsRequest(int requestorId, int serverId, String machineId, MachineSnapshotManagementAction action, String name, String description, String id)
    {
        setRequestorId(requestorId);
        setServerId(serverId);
        setMachineId(machineId);
        this.snapshotManagementAction = action;
        this.snapshotName = name;
        this.snapshotDescription = description;
        this.snapshotId = id;
        setRequestedAction(PermissionsConstants.ACTION_MACHINE_SNAPSHOT_MANAGE);
    }

    /**
     * Constructs a request for the creation of a new snapshot of the specified machine.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the machine resides
     * @param machineId the id of the snapshot parent machine
     * @param name name for the new snapshot
     * @param description description for the new snapshot
     */
    public ManageMachineSnapshotsRequest(int requestorId, int serverId, String machineId, String name, String description)
    {
        this(requestorId, serverId, machineId, MachineSnapshotManagementAction.CREATE, name, description, null);
    }

    /**
     * Constructs a request for the modification of a snapshot on the specified machine.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the machine resides
     * @param machineId the id of the snapshot parent machine
     * @param name the new snapshot name
     * @param description the new snapshot description
     * @param id the UUID of the snapshot
     */
    public ManageMachineSnapshotsRequest(int requestorId, int serverId, String machineId, String name, String description, String id)
    {
        this(requestorId, serverId, machineId, MachineSnapshotManagementAction.MODIFY, name, description, id);
    }

    /**
     * Constructs a request for the removal/restoration of a snapshot on the specified machine
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the machine resides
     * @param machineId the id of the snapshot parent machine
     * @param id the id of the snapshot
     * @param action the requested action (remove/restore)
     *
     * @see MachineSnapshotManagementAction#REMOVE
     * @see MachineSnapshotManagementAction#RESTORE
     */
    public ManageMachineSnapshotsRequest(int requestorId, int serverId, String machineId, String id, MachineSnapshotManagementAction action)
    {
        this(requestorId, serverId, machineId, action, null, null, id);
    }

    /**
     * Retrieves the snapshot management action associated with this request.
     *
     * @return the snapshot management action
     */
    public MachineSnapshotManagementAction getSnapshotManagementAction()
    {
        return snapshotManagementAction;
    }

    /**
     * Retrieves the name of the snapshot.
     *
     * @return the name of the snapshot, if available or <code>null</code> otherwise
     */
    public String getSnapshotName()
    {
        return snapshotName;
    }

    /**
     * Retrieves the description of the snapshot.
     *
     * @return the snapshot description, if available or <code>null</code> otherwise
     */
    public String getSnapshotDescription()
    {
        return snapshotDescription;
    }

    /**
     * Retrieves the id of the snapshot.
     *
     * @return the snapshot id, if available or <code>null</code> otherwise
     */
    public String getSnapshotId()
    {
        return snapshotId;
    }

    /**
     * Sets the snapshot management action for the request.
     *
     * @param snapshotManagementAction the snapshot management action
     */
    public void setSnapshotManagementAction(MachineSnapshotManagementAction snapshotManagementAction)
    {
        this.snapshotManagementAction = snapshotManagementAction;
    }

    /**
     * Sets the snapshot name for the request.
     *
     * @param snapshotName the snapshot name
     */
    public void setSnapshotName(String snapshotName)
    {
        this.snapshotName = snapshotName;
    }

    /**
     * Sets the snapshot description for the request.
     *
     * @param snapshotDescription the snapshot description
     */
    public void setSnapshotDescription(String snapshotDescription)
    {
        this.snapshotDescription = snapshotDescription;
    }

    /**
     * Sets the snapshot id for the request.
     *
     * @param snapshotId the snapshot id (UUID)
     */
    public void setSnapshotId(String snapshotId)
    {
        this.snapshotId = snapshotId;
    }

    /**
     * Checks if the supplied data is valid, based on the snapshot management action.
     *
     * @return <code>true</code> if the data is valid or <code>false</code> otherwise
     */
    private boolean isDataValid()
    {
        boolean result = false;

        if(snapshotManagementAction == null)
            return false;
        else
            ;

        switch(snapshotManagementAction)
        {
            case CREATE:
            {
                if(snapshotName != null && !snapshotName.equals(""))
                    result = true;
                else
                    ;
            } break;

            case MODIFY:
            {
                if(snapshotName != null && !snapshotName.equals("")
                   && Functions.isUUIDValid(snapshotId))
                    result = true;
                else
                    ;
            } break;

            case REMOVE:
            case RESTORE:
            {
                if(Functions.isUUIDValid(snapshotId))
                    result = true;
                else
                    ;
            } break;

            default: result = false;
        }

        return result;
    }

    @Override
    public boolean isValid()
    {
        if(super.isValid() && isDataValid())
            return true;
        else
            return false;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (machineId,snapshotManagementAction,
     * snapshotName,snapshotDescription,snapshotId)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return ManageMachineSnapshotsRequest.class.getSimpleName()
                + ": (" + getMachineId() + "," + snapshotManagementAction
                + "," + snapshotName + "," + snapshotDescription + "," + snapshotId + ")";
    }
}
